// pages/search/search.js
var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0,//任务类型
    id:"",
    oldId:"",
    copyId:"",
    keyShowed: 0,
    searchName: "",
    keyword: "",
    completeTime: "",//排程外访日期
    collerName: "",//催收员姓名
    historySearchName: [],//历史搜索记录
    totalPage: 0,//总页数
    nowPage: 1,//当前页码
    pagesize: 10,//页大小
    taskList: [],//任务列表
    outvisitId: 0,//当前的任务id
    delayFormIsHide: true,//延时外访表单是否隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var type = options.type;
    var id = options.id;

    // 设置任务类型
    that.setData({
      type: type,
      id: id,
      oldId:id,
    });

    that.getTaskList();
    // 设置历史搜索记录
    //that.setHistorySearch();
  },

  // 设置历史搜索记录
  setHistorySearch: function () {
    var keyShowed = 0;
    var historySearchName = wx.getStorageSync('historySearchName');
    if (historySearchName.length == 0) {
      keyShowed = 2;
    }
    if (historySearchName == undefined || historySearchName == "") {
      historySearchName = [];
      wx.setStorageSync('historySearchName', []);
    }
    this.setData({
      historySearchName: historySearchName,
      keyShowed: keyShowed
    });
  },

  // 搜索
  search: function (e) {
    var that = this;
    console.log("e.detail.value=========>");
    console.log(e.detail.value);

    var keyword = e.currentTarget.dataset.keyword;
    console.log("keyword=======>"+keyword);

    //添加历史搜索记录
    var historySearchName = wx.getStorageSync('historySearchName');
    if (e.detail.value) {
      historySearchName.push(e.detail.value);
      wx.setStorageSync('historySearchName', historySearchName);
    }
    // 清空列表
    that.setData({
      keyShowed: 1,
      keyword: e.detail.value,
      historySearchName: historySearchName,
      nowPage: 1
    },res => {
      // 获取任务列表
      that.getTaskList();
    });
  },

  searchByKeyword: function (e) {
    var that = this;
    var keyword = e.currentTarget.dataset.keyword;
    //添加历史搜索记录
    var historySearchName = wx.getStorageSync('historySearchName');
    if (e.detail.value) {
      historySearchName.push(e.detail.value);
      wx.setStorageSync('historySearchName', historySearchName);
    }
    // 清空列表
    that.setData({
      keyShowed: 1,
      keyword: keyword,
      historySearchName: historySearchName,
      nowPage: 1
    },res => {
      // 获取任务列表
      that.getTaskList();
    });
  },

  surveyDetails: function(e) {
    var that = this;
    console.log("到这里了");
    var copyid = e.currentTarget.dataset.copyid;//当前文件的下标
    console.log(copyid);

    api.copyValues(that.data.id, copyid).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        that.setData({
          delayFormIsHide: true
        })

        var pages = getCurrentPages(); // 当前页面
        var beforePage = pages[pages.length - 2]; // 前一个页面
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
            changed: true,
            success: function () {
              wx.showToast({
                title: "复制成功",
                icon: "none"
              });
              //beforePage.onLoad(); // 执行前一个页面的onLoad方法
            }
          });
        }, 1000);
      }
    });
  },

  // 历史搜索
  historySearch: function (e) {
    var that = this;
    // 清空列表
    var searchName = e.currentTarget.dataset.serachname;
    that.setData({
      keyShowed: 1,
      searchName: searchName,
      nowPage: 1
    }, res => {
      // 获取任务列表
      that.getTaskList();
    });
  },

  // 获取任务列表
  getTaskList: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });//显示加载中
    api.getAllTaskList(that.data.nowPage, that.data.pagesize, that.data.state, that.data.keyword).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        var dataList = res.data;
        var taskList = [];
        taskList = res.data.data;
        for (var i = 0; i < dataList.length; i++) {
          var data = dataList[i];
          data.delay = false;
          if (that.data.type == 1 && data.completeTime) {
            var timestamp1 = new Date(data.completeTime).getTime();
            var timestamp2 = new Date(util.formatTimeTT(new Date().getTime(), "Y-M-D")).getTime();
            if (timestamp2 - timestamp1 >= 2 * 24 * 60 * 60 * 1000) {
              data.delay = true;
            }
          }
          taskList.push(data);
        }
        that.setData({
          //totalPage: res.data.page.totalPage,
          taskList: taskList
        });
      }
    });
  },

  // 清空历史搜索记录
  clearhistory: function () {
    wx.setStorageSync('historySearchName', []);
    this.setData({
      historySearchName: [],
      keyShowed: 2
    })
  },

  // 清空搜索框
  clearInput: function () {
    this.setData({
      searchName: "",
      keyShowed: 0,
    });
  },

  inputTyping: function (e) {
    this.setData({
      searchName: e.detail.value,
    });
  },

  //显示筛选案件
  filter: function () {
    this.setData({
      statusshow: true
    });
  },

  //设置催收员姓名
  inputColler: function (e) {
    this.setData({
      collerName: e.detail.value,
    });
  },

  //重置筛选
  filterReset: function () {
    var that = this;
    that.setData({
      completeTime: "",
      collerName: ""
    });
  },

  //确认筛选
  filterhide: function () {
    var that = this;
    that.setData({
      statusshow: false
    });
    // 刷新列表
    that.updateList();
  },

  // 刷新列表
  updateList: function () {
    var that = this;
    that.setData({
      keyShowed: 1,
      nowPage: 1
    }, res => {
      // 获取任务列表
      that.getTaskList();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.nowPage < that.data.totalPage) {
      that.setData({
        nowPage: that.data.nowPage + 1
      }, res => {
        // 获取任务列表
        that.getTaskList();
      });
    } else {
      wx.showToast({
        title: '没有更多数据',
        icon: 'none'
      });
    }
  },

  //时间选择
  bindDateChange(e) {
    let that = this;
    that.setData({
      completeTime: e.detail.value
    })
  },

  // 延时外访
  delay: function (e) {
    var that = this;
    var cause = e.detail.value.cause;
    if (!cause.trim()) {
      wx.showToast({
        title: "延时原因不能为空",
        icon: "none"
      });
      return;
    }
    wx.showLoading();//显示加载中
    api.delayedOut(that.data.outvisitId, cause).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        that.setData({
          delayFormIsHide: true
        })
        wx.showToast({
          title: "延时成功",
          icon: "none"
        });
        setTimeout(() => {
          // 刷新列表
          that.updateList();
        }, 500);
      }
    });
  },

  // 延时外访表单显示与隐藏
  delayFormShowOrHide: function (e) {
    var delayFormIsHide = this.data.delayFormIsHide;
    if (delayFormIsHide) {
      var delayedCount = e.currentTarget.dataset.delayedcount;
      if (delayedCount >= 2) {
        wx.showToast({
          title: "最多只能申请两次延时外访",
          icon: "none"
        });
        return;
      }
      delayFormIsHide = false;
      this.setData({
        outvisitId: e.currentTarget.dataset.id
      });
    } else {
      delayFormIsHide = true;
    }
    this.setData({
      delayFormIsHide: delayFormIsHide
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
